package com.threeti.jiayusms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.threeti.jiayusms.domain.model.CommunicationVoice;
import com.threeti.jiayusms.domain.service.communicationvoice.CommunicationVoiceQueryService;
import com.threeti.jiayusms.domain.service.communicationvoice.CommunicationVoiceUpdateService;
import com.threeti.jiayusms.domain.util.TemplateIdGenerator;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.VoiceTemplateForm;

@Controller
public class VoiceTempleteMainController {

	@Autowired
	private CommunicationVoiceQueryService queryService;
	
	@Autowired
	private CommunicationVoiceUpdateService updateService;

	 @RequestMapping(value="/voiceTemplateMain.html")
	  public String enterVoiceTemplateMain(HttpServletRequest request,Map modelMap) {
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationVoice> pagedVoiceTemplates = queryService.findAllVoiceTemplatesByPage(index[0], index[1]);
	    pagedHelper.processPage(pagedVoiceTemplates, request);
	    
	    modelMap.put("voiceTemplates", pagedVoiceTemplates.getPageList());
	    
	    return "voicetemplate/voiceTemplateMain";
	  }
	
	 @RequestMapping(value="/newVoiceTemplate.html", method=RequestMethod.POST)
	  public String newVoiceTemplate(@ModelAttribute VoiceTemplateForm form) {
	  	CommunicationVoice voiceTemplate = new CommunicationVoice();

	  	voiceTemplate.setVoiceDescription(form.getVoiceDescription());
	  	voiceTemplate.setVoiceContent(form.getVoiceContent());
	  	
	  	CommunicationVoice rtnVoiceTemplate = updateService.storeCommunicationVoice(voiceTemplate);
	  	String templateId = TemplateIdGenerator.generateTempalteIdBy("YY", rtnVoiceTemplate);
	  	rtnVoiceTemplate.setCustomId(templateId);
	  	updateService.storeCommunicationVoice(rtnVoiceTemplate);
	    
	    return "redirect:voiceTemplateMain.html";
	  }
	 
	 @RequestMapping(value="/removeVoiceTemplate.html", method = RequestMethod.POST)
	  public String removeVoiceTemplate(@RequestParam("keys") String pks,
	                            Map modelMap, HttpServletRequest request) {
	    String[] voiceTemplateIds = pks.split(",");

	    for (int i = 0; i < voiceTemplateIds.length; i++) {
	      updateService.removeCommunicationVoiceBy(Long.valueOf(voiceTemplateIds[i]));
	    }
	    return "redirect:/voiceTemplateMain.html";
	  }
	 
	 
	  @RequestMapping(value="/enterVoiceTemplateEditForm.html", method = RequestMethod.GET)
	  public String enterEditVoiceTemplate(@RequestParam("updatekey") String voiceTemplateId,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationVoice template = queryService.findCommunicationVoiceBy(Long.valueOf(voiceTemplateId));
	  	
	  	modelMap.put("voiceTemplate", template);
	    
	    return "voicetemplate/editVoiceTemplateForm";
	  }
	  
	  @RequestMapping(value="/editVoiceTemplate.html", method = RequestMethod.POST)
	  public String editVoiceTemplate(@ModelAttribute VoiceTemplateForm form,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationVoice template = queryService.findCommunicationVoiceBy(Long.valueOf(form.getUpdatekey()));
	  	
	  	template.setVoiceDescription(form.getVoiceDescription());
	  	template.setVoiceContent(form.getVoiceContent());
	  	
	  	updateService.storeCommunicationVoice(template);
	  	
	  	modelMap.put("voiceTemplate", template);
	    
	    return "redirect:/voiceTemplateMain.html";
	  }
	  
	  
	  @RequestMapping(value="/searchVoiceTemplateByPage.html")
	  public String searchVoiceTemplate(HttpServletRequest request, @ModelAttribute VoiceTemplateForm form, Map modelMap) {

	  	DynaSearchCriteria dynaSearchCriteria = null;
	  	
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	  	if (pagedHelper.isAnyPageSearch(request)) {
	  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(VoiceTemplateForm.SESSION_DYNA_CRITERIA_KEY);
	  	} else {
	      modelMap.put(VoiceTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.VoiceTemplateDynaCriteriaDto")));  		
	  	}
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationVoice> pagedVoiceTemplates = queryService.findVoiceTemplatesByPage(dynaSearchCriteria, index[0], index[1]);
	    pagedHelper.processPage(pagedVoiceTemplates, request);
	    
	    modelMap.put("voiceTemplates", pagedVoiceTemplates.getPageList());
	    
	    return "voicetemplate/voiceTemplateMain";
	  }
	  
}
